package com.cks.eduservice.service.impl;

import com.cks.commonutils.R;
import com.cks.eduservice.entity.EduCourse;
import com.cks.eduservice.entity.EduCourseDescription;
import com.cks.eduservice.entity.vo.CourseInfoVo;
import com.cks.eduservice.entity.vo.CoursePublishVo;
import com.cks.eduservice.mapper.EduCourseMapper;
import com.cks.eduservice.service.EduChapterService;
import com.cks.eduservice.service.EduCourseDescriptionService;
import com.cks.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cks.eduservice.service.EduVideoService;
import com.cks.servicebase.exceptionhandler.CksException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-01-18
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {


    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    @Override
    public String addCourseInfo(CourseInfoVo courseInfoVo) {
        //向课程表加课程表基本信息
        //courseInfoVo 对象转换 EduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
//        System.out.println(courseInfoVo);
//        System.out.println(eduCourse);
        //baseMapper
        int insert = baseMapper.insert(eduCourse);
        if(insert==0){
            throw  new CksException(2001,"添加课程信息失败");
        }
            String id = eduCourse.getId();
            //向课程简介表添加课程简介
            EduCourseDescription eduCourseDescription = new EduCourseDescription();
            eduCourseDescription.setDescription(courseInfoVo.getDescription());
            eduCourseDescription.setId(id);
            eduCourseDescriptionService.save(eduCourseDescription);
           return id;
    }

    @Override
    public CourseInfoVo getCouresInfo(String couresId) {
        //查询课程表中的信息
        EduCourse eduCourse = baseMapper.selectById(couresId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //查询描述表并把信息放入到
        EduCourseDescription courseDescriptionServiceById = eduCourseDescriptionService.getById(couresId);
        courseInfoVo.setDescription(courseDescriptionServiceById.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourserInfo(CourseInfoVo courseInfoVo) {

        //修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = baseMapper.updateById(eduCourse);

        if(i==0){
            throw  new CksException(2001,"修改课程信息失败");
        }
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        boolean b = eduCourseDescriptionService.updateById(eduCourseDescription);
        System.out.println(12312);

    }

    @Override
    public CoursePublishVo getPublishCourInfo(String id) {
        //调用Mapper

        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    public void removeCourse(String id) {
        //根据课程id删除小节
        eduVideoService.removeVideoById(id);
        //根据课程id删除章节
        eduChapterService.removeChapterById(id);
        //根据课程id删除描述因为描述id和课程id 是一样的用removeById
        eduCourseDescriptionService.removeById(id);
        //根据课程id删除课程本身
        int i = baseMapper.deleteById(id);
        if(i==0){
         throw new CksException(2001,"删除失败");
        }
    }
}
